//
//  Util.swift
//  beegin
//
//  Created by zsy on 15/9/6.
//  Copyright (c) 2015年 kszz. All rights reserved.
//

import Foundation


let ElementsDetail = [("钠","mg"),("钾","mg"),("铜","mg"),("镁","mg"),("铁","mg"),
    ("锌","mg"),("锰","μg"),("钙","mg"),("磷","mg"),("碘","μg"),
    ("氯","mg"),("硒","μg"),("氟","μg")]


let beeginCustomer = BeeginCusomer()


class Util {
    
    
    class func PYFirst(string:String?, _ allFirst:Bool=false)->String
     {
        var py="#"
        if let s = string
        {
            if s == ""
        
            {
                return py
            }
            
            let str = CFStringCreateMutableCopy(nil, 0, s)
            CFStringTransform(str, nil, kCFStringTransformToLatin, false)
            CFStringTransform(str, nil, kCFStringTransformStripCombiningMarks, false)
            py = ""
            if allFirst
            {
                for x in (str as String).componentsSeparatedByString(" ")
                {
                    py += PYFirst(x)
                }
            } else {
                
                py = (str as NSString).substringToIndex(1).uppercaseString
            }
        }
        return py
    }
    
    class func FliterChineseStringArr(arr:[AnyObject],keyword:String = "name")->[(String,[AnyObject])] {
    
        var reslut:[(String,[AnyObject])] = []
        var tempSorted:[(String,AnyObject)] = []
        
        for item in arr{
            var pinYinResult:String = ""
            
            if let keyString = item.valueForKey(keyword) as? String{
            
                for hanzi in keyString.characters{
                    let pinyin = PYFirst(String(hanzi), false)
                    
                    if !pinYinResult.containsString(pinyin){
                        pinYinResult += pinyin
                    }
                }
            }
        
            tempSorted.append((pinYinResult,item))
//            print("pinYinResult:\(pinYinResult)")
        }
        
        tempSorted.sortInPlace{
        
            return $0.0.compare($1.0) == NSComparisonResult.OrderedAscending
        }
        for item in tempSorted{
        
            if let last = reslut.last{
            
                if (last.0 as NSString).substringToIndex(1) == (item.0 as NSString).substringToIndex(1){
                
                    reslut[reslut.count - 1].1.append(item.1)
                    
                    continue
                }
            }
            
            let  header = (item.0 as NSString).substringToIndex(1)
            
            reslut.append((header,[item.1]))
        }
        
        return reslut
    }
    
    
    class func FliterChineseString<T>(arr:[T],keyword:String = "name")->[(String,[T])] {
        
        var reslut:[(String,[T])] = []
        var tempSorted:[(String,T)] = []
        
        for item in arr{
            var pinYinResult:String = ""
            
            
            if let keyString = (item as? AnyObject)?.valueForKey(keyword) as? String{
                
                for hanzi in keyString.characters{
                    let pinyin = PYFirst(String(hanzi), false)
                    
                    if !pinYinResult.containsString(pinyin){
                        pinYinResult += pinyin
                    }
                }
            }
            
            tempSorted.append((pinYinResult,item))
        }
        
        tempSorted.sortInPlace{
            
            return $0.0.compare($1.0) == NSComparisonResult.OrderedAscending
        }
        for item in tempSorted{
            
            if let last = reslut.last{
                
                if (last.0 as NSString).substringToIndex(1) == (item.0 as NSString).substringToIndex(1){
                    
                    reslut[reslut.count - 1].1.append(item.1)
                    
                    continue
                }
            }
            
            let  header = (item.0 as NSString).substringToIndex(1)
            
            reslut.append((header,[item.1]))
        }
        
        return reslut
    }
    
    class func TransBirthdayOfAge(birthday:String)->String{
        
        let format = NSDateFormatter()
        format.dateFormat = "yyyy-MM-dd"
        let babyAge = format.dateFromString(birthday)
        
        let interval:Int = Int(NSDate().timeIntervalSinceDate(babyAge!))
        let daySec = 60 * 60 * 24
        let yearSec = daySec * 365
        let mothSec = daySec * 30
        let years = Int(interval / yearSec)
        let moth = Int((interval - (years * yearSec)) / mothSec)
        
       return "\(years)岁\(moth)月"
    }
    
    class func TransBirthdayOfStages(birthday:String)->Int{
    
        let format = NSDateFormatter()
        format.dateFormat = "yyyy-MM-dd"
        let babyAge = format.dateFromString(birthday)
        
        let interval:Int = Int(NSDate().timeIntervalSinceDate(babyAge!))
        let weekSec = 60 * 60 * 24 * 7
        
        if interval < weekSec * 6{
        
            return 0
        }else
            if interval < weekSec * 12{
        
                return 1
        }else
                if interval < weekSec * 24{
        
                    return 2
        }else
                    if interval < weekSec * 36{
        
                        return 3
        }
        
        return 4
    }
    
   }